package cn.itsource.order.service.impl;

import cn.itsource.basic.config.BussinessException;
import cn.itsource.order.domain.OrderCancleLog;
import cn.itsource.order.domain.OrderPetAcquisition;
import cn.itsource.order.dto.OrderConfirmDto;
import cn.itsource.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.order.service.IOrderCancleLogService;
import cn.itsource.order.service.IOrderPetAcquisitionService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.service.IPetService;
import cn.itsource.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IPetService petService;
    @Autowired
    private IOrderCancleLogService orderCancleLogService;

    @Override
    public void confirm(OrderConfirmDto orderConfirmDto) {
        // 1.校验订单是否存在
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderConfirmDto.getId());
        if (orderPetAcquisition == null) {
            throw new BussinessException("此订单不存在来了");
        }
        // 2.查询寻主信息修改状态为已完成
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null) {
            throw new BussinessException("此寻主消息已不存在");
        }
        searchMasterMsg.setState(3);
        searchMasterMsgService.update(searchMasterMsg);
        // 3.保存宠物信息，并且设置成本的价格【员工填写的实际价格】
        Pet pet = initPet(orderConfirmDto, orderPetAcquisition, searchMasterMsg);
        // 4.更新订单状态和价格，并且加上pet的id
        initOrder(orderConfirmDto, orderPetAcquisition, pet);
    }

    /**
     * 更新订单
     * @param orderConfirmDto
     * @param orderPetAcquisition
     * @param pet
     */
    private void initOrder(OrderConfirmDto orderConfirmDto, OrderPetAcquisition orderPetAcquisition, Pet pet) {
        orderPetAcquisition.setPrice(orderConfirmDto.getMoney());
        orderPetAcquisition.setState(3);
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }

    /**
     * 初始化宠物表
     * @param orderConfirmDto
     * @param orderPetAcquisition
     * @param searchMasterMsg
     * @return
     */
    private Pet initPet(OrderConfirmDto orderConfirmDto, OrderPetAcquisition orderPetAcquisition, SearchMasterMsg searchMasterMsg) {
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(orderConfirmDto.getMoney().toString());
        pet.setTypeId(1L);
        pet.setShopId(orderPetAcquisition.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petService.add(pet);
        return pet;
    }

    @Override
    public void cancel(Long orderId) {
        // 1.根据Id查询订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderId);
        if (orderPetAcquisition == null){
            throw new BussinessException("订单不存在！");
        }
        // 2.查询寻主信息，修改状态，清空shopId
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        searchMasterMsg.setState(1);
        searchMasterMsg.setShopId(null);
        searchMasterMsgService.update(searchMasterMsg);
        // 3.修改订单状态为取消
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 4.保存取消日志
        OrderCancleLog orderCancleLog = new OrderCancleLog();
        orderCancleLog.setOrderId(orderId);
        orderCancleLog.setCancelShopid(orderPetAcquisition.getShopId());
        orderCancleLogService.add(orderCancleLog);
    }
}
